<template>
	<view class="zone">
		<Header title="我的银元" color="#fff"></Header>
		<view class="main">
			<view class="top">
				<view class="t_top">
					<view class="left">
						<p>剩余银元</p>
						<h3>{{userinfo.silver}}</h3>
					</view>
					<image src="http://img.cpgm.cc/panda/static/self/bigyb.png" mode="heightFix" />
				</view>
				<p class="explain">银元可以通过宝石、水晶兑换获得; <br>
				也可以通过推广、做任务、打装备换取</p>
			</view>

			<scroll-view scroll-y class="scrollbox"  @scrolltolower="scrolltolower" >
				<view class="list">
					<view class="item" v-for="(item, index) in datalist" :key="index">
						<view class="left">
							<p>{{item.remark}}</p>
							<p>{{item.created_at}}</p>
						</view>
						<view class="right">{{item.operation_type == 1?'+':'-'}}{{item.number}}</view>
					</view>		

					<view v-if="datalist.length==0" class="empty">
						<image src="http://img.cpgm.cc/panda/static/public/empty.png" class="zaneu_img" mode="widthFix"></image>
						<text>没有数据哦~</text>
					</view>
					<u-loadmore v-else :status="status" :icon-type="iconType" :load-text="loadText" />			
				</view>
					
				</scroll-view>

		
			<view class="button exchange" @click="giveshow = true">
				银元转赠
			</view>
		</view>


		<!-- 银元转赠 -->
		<u-mask :show ="giveshow">
			<view class="playbox" :class="[giveshow?'playboxac':'']">
				<image class="closed2" @click="giveshow = false" src="http://img.cpgm.cc/panda/static/public/close.png" mode="widthFix" />
				<view class="content">
					<image class="yb" src="http://img.cpgm.cc/panda/static/self/bigyb.png" mode="widthFix" />
					<h3>银元转赠</h3>
					<p class="explain">我的银元 <text>{{userinfo.silver}}</text>个</p>
					<view class="piece">
						<text>好友ID</text>
						<input type="number" v-model="invite_code" @blur="blurchange()" maxlength="6" placeholder="请输入好友ID" placeholder-class="ipttxt" />
						<view class="people">
							<image :src="pimg" mode="" />
							<text>{{name}}</text>
						</view>
					</view>
					<view class="piece">
						<text>赠送数量</text>
						<input type="number" @input="inputchange()" v-model="numdata" placeholder="请输入赠送数量" placeholder-class="ipttxt" />
					</view>
					<view class="piece">
						<text>手续费</text>
						<input type="number" disabled="disabled" v-model="servicecharge"
								placeholder-class="ipttxt" />
					</view>
					<p class="tips">请确认接收人ID，赠送后无法追回 <br>
							平台禁止银元倒卖出售，一经发现严肃处理</p>

					<view class="allmoney">合计银元：<text>{{amount}}</text>个</view>
					<view class="button" @click="giveGold">立即转赠</view>
				</view>
			</view>
		</u-mask>


		<!-- 是否确认赠送 -->
		<u-mask :show="sureshow">
			<view class="warp">
				<view class="givebox">
					<h3>银元转赠</h3>
					<view class="m_content2">
						<p class="centerdd">赠送后无法退回,请谨慎操作</p>
						<p class="centerdd">确定向ID{{invite_code}}的用户赠送{{numdata}}枚银元吗</p>
					</view>
					<!-- <view class="button quit" @click="sureshow = false">取消</view> -->
					<u-button :plain="true" :hair-line="false" hover-class="none" :throttle-time="1000"
						class="button" @click="givegoldsure()">确认</u-button>
				</view>
				<image class="closed" @click="sureshow = false" src="http://img.cpgm.cc/panda/static/public/close.png" mode="widthFix" />
			</view>
		</u-mask>



	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				giveshow:false,
				sureshow:false,
				numdata: '', //赠送水晶数量
				invite_code: '', //赠送好友ID
				servicecharge: 0,
				selecet: false, //是否选择
				rate: '',
				amount: 0,
				charge: '',
				userinfo: uni.getStorageSync('userinfo'),
				page: 1,
				max_page: 1,
				status: 'loadmore',
				iconType: 'flower',
				loadText: {
					loadmore: '上拉加载',
					loading: '努力加载中',
					nomore: '没有更多了'
				},
				datalist: [],
				name:'',
				pimg:'',
			};
		},
		onLoad(){
			this.getUserInfo();
			this.getSilverList();
		},
		methods: {
			// 获取个人信息
			getUserInfo(){
				this.$getUserInfo.getUserInfo((res)=>{
					if(res.code == 1) {
						this.userinfo = res.data
						this.charge = res.data.silver_rate
					}
				})
			},

			// 获取银元明细
			async getSilverList() {
				let res = await this.$http.index.getSilverList({
					page: this.page
				})
				this.datalist = [...this.datalist, ...res.data];
				this.max_page = res.page;
				if (this.page >= this.max_page) {
					this.status = 'nomore'
				}
			},

			// 失去焦点时触发
			blurchange(){
				if(this.invite_code.length == 6) {
					this.getUInfo()
				}else {
					this.$u.toast('请输入正确的ID')
				}
			},

			async getUInfo(){
				let res = await this.$http.index.getUInfo({
					invite_code:this.invite_code
				})
				if(res.code == 1) {
					console.log(res);
					this.pimg = res.data.img
					this.name = res.data.nickname
				}else {
					this.$u.toast(res.msg)
				}
			},

			async getSilverList2() {
				let res = await this.$http.index.getSilverList({
					page: 1
				})
				this.datalist = res.data;
				this.max_page = res.page;
				if (this.page >= this.max_page) {
					this.status = 'nomore'
				}
			},

			scrolltolower() {
				if (this.page >= this.max_page) {
					this.status = 'nomore'
				} else {
					this.status = 'loading';
					this.page = ++this.page;
					this.getSilverList();
					setTimeout(() => {
						if (this.page > this.max_page || this.page == this.max_page) this.status = 'nomore';
						else this.status = 'loading';
					}, 500)
				}
			},
			
			// 监听用户输入水晶数量逻辑
			inputchange(e){
				let change =  Number(this.userinfo.silver) / (1 + Number(this.charge)) 
				// 最大可赠送数量
				change = Math.floor(change)

				if(this.numdata > change) {
					this.$u.toast('赠送数量不能大于总计数量')
					this.$nextTick(() => {
						this.numdata = change
					});
				}
				
				this.$nextTick(()=>{
					this.numdata = this.numdata
					if(this.numdata == '' || this.numdata == 'null' || this.numdata == 0){
						this.servicecharge = 0
						this.amount = 0
					}else {
						this.servicecharge = (this.numdata * this.charge).toFixed(2) 
						this.amount =  (Number(this.numdata) + Number(this.servicecharge)).toFixed(2) 
						if(isNaN(this.amount)) {
							this.amount = 0
						}
						if(isNaN(this.servicecharge)) {
							this.servicecharge = 0
						}
					}
				})
			},


			// 赠送好友水晶
			async giveGold() {
				if (this.numdata == '' || this.numdata == null) {
					this.$u.toast('请输入赠送银元数量')
				} else if (this.invite_code == '' || this.invite_code == null) {
					this.$u.toast('请输入赠送的好友ID')
				} else {
					this.sureshow = true
				}
			},

			// 赠送好友水晶
			async givegoldsure() {
				let res = await this.$http.index.giveSilver({
					invite_code: this.invite_code,
					num: this.numdata,
					trader: this.danbao,
				})
				this.$u.toast(res.msg)
				if (res.code == 1) {
					this.getUserInfo();
					this.getSilverList2();
					this.giveshow = false
					this.sureshow = false
				}
			},

		},
	}
</script>

<style lang="less">
.zone {
	background: linear-gradient(180deg, #0477FD 0%, #7AFDCF 100%);
	min-height: 100vh;
}

.main {
	padding: 10px ;
}

.top {
	background: linear-gradient(90deg, #4F4E4C 0%, #242424 100%);
	box-shadow: 0px 5px 8px 0px rgba(0,0,0,0.2), inset -4px 0px 5px 0px rgba(53,53,53,1), inset 0px -4px 5px 0px rgba(77,77,77,1);
	border-radius: 13px;
	padding: 15px;

	.source {
		color: rgba(255, 255, 255, 0.6);
		font-size: 14px;
		font-weight: 600;
		margin-top: 20px;
	}
}

.t_top {
	display: flex;
	align-items: center;
	justify-content: space-between;

	.left {
		display: flex;
		flex-direction: column;

		p {
			color: #FFFFFF;
			font-size: 13px;
			
		}
		h3 {
			color: #FFFFFF;
			font-size: 30px;
			margin-top: 4px;
		}
	}

	image {
		height: 65px;
	}
}

.list {
	padding: 10px 0 50px;
}

.scrollbox {
	height: calc(100vh - 310px);

	.item {
		background: rgba(255, 255, 255, 0.8);
		border-radius: 8px;
		padding: 10px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 8px;

		.left {
			p:nth-child(1) {
				color: #131314;
				font-size: 14px;
				font-weight: 600;
			}
			p:nth-child(2) {
				color: #333333;
				font-size: 12px;
				opacity: 0.8;
				margin-top: 4px;
			}
		}

		.right {
			font-size: 14px;
			font-weight: 600;
			color: #333333;
		}
	}
}



.exchange {
	width: 80%;
	left: 50%;
	transform: translateX(-50%);
	box-sizing: border-box;
	max-width: 400px;
	position: fixed;
	bottom: 30px;
	display: flex;
	height: 60px;
	flex-direction: column;
	align-items: center;
	background: url('http://img.cpgm.cc/panda/static/public/btn3.png');
	background-size: 100% 100%;
	padding: 10px;
	color: #B73800;
	font-size: 20px;
	font-weight: 600;
}


// 转赠


.playbox {
	width: 100%;
	position: absolute;
	left: 0%;
	bottom: -50vh;
	background: url('http://img.cpgm.cc/panda/static/mask/bbg.png');
	background-size: 100% 100%;
	transition: all 0.3s linear;

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 20px;

		.yb {
			width: 32%;
			margin-top: -18%;
		}

		h3 {
			position: relative;
			color: #000000;
			font-size: 20px;
			width: 50%;
			text-align: center;
			z-index: 2;
		}

		.explain {
			color: #333333;
			font-size: 14px;
			margin: 10px 0;
			text {
				font-weight: 600;
			}
		}

		h3::after {
			content: '';
			width: 100%;
			height: 30px;
			background: url('http://img.cpgm.cc/panda/static/mask/banner.png');
			background-size: 100% auto;
			background-position: center;
			background-repeat: no-repeat;
			position: absolute;
			top: 0;
			left: 0;
			z-index: -1;
		}

		.piece {
			width: 100%;
			background: #FFFFFF;
			border-radius: 9px;
			padding: 15px;
			display: flex;
			align-items: flex-start;
			margin: 5px 0;
			position: relative;

			text {
				color: #1D1E24;
				font-size: 14px;
				font-weight: 600;
				margin-right: 10px;
			}

			input {
				text-align: left;
				flex: 0.9;
				margin-left: 10px;
			}
		}

		.allmoney {
			margin-top: 20px;
			color: #696969;
			font-size: 14px;
			font-weight: 600;

			text {
				color: #000;
			}
		}

		.button {
			height: 68px;
			width: 100%;
			background: url('http://img.cpgm.cc/panda/static/public/btn3.png');
			background-size: 100% 100%;
			color: #B73800;
			font-size: 18px;
			font-weight: 600;
			margin-top: 10px;
		}

	}
	.closed2 {
		position: absolute;
		width: 25px;
		top: 8%;
		right: 15px;
	}

	.tips {
		width: 100%;
		text-align: left;
		color: #696969;
		font-size: 12px;
		margin-top: 5px;
	}
}

.playboxac {
	bottom:0 !important;
}



// 转赠弹窗

.givebox {
	background: linear-gradient(180deg, #61FBED 0%, #E4FDFC 31%, #F6FEEB 68%, #F5FEA8 100%);
	box-shadow: inset 5px 5px 45px 0px rgba(255,255,255,0.5), inset -5px -5px 45px 0px rgba(255,255,255,0.5), inset 0px 2px 0px 0px rgba(255,255,255,0.5);
	padding: 15px;
	background-size: 100% 100%;
	border-radius: 14px;
	display: flex;
	flex-direction: column;
	align-items: center;

	h3 {
		position: relative;
		color: #000000;
		font-size: 20px;
		width: 50%;
		text-align: center;
		z-index: 2;
	}
	h3::after {
		content: '';
		width: 100%;
		height: 30px;
		background: url('http://img.cpgm.cc/panda/static/mask/banner.png');
		background-size: 100% auto;
		background-position: center;
		background-repeat: no-repeat;
		position: absolute;
		top: 0;
		left: 0;
		z-index: -1;
	}

	.button {
		width: 80%;
		height: 55px;
		color: #B73800;
		margin: 0 auto;
		margin-top: 20px;
		background: url('http://img.cpgm.cc/panda/static/public/btn3.png');
		background-size: 100% 100%;
		font-weight: 600;
	}
}


.button2 {
	color: #239D4C;
	text-decoration: underline;
	font-size: 14px;
	font-weight: 600;
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;

	image {
		width: 13px;
		margin-left: 5px;
	}

	.seposabox {
		position: absolute;
		top: 100%;
		left: 0;
		transform: translateX(-32%);
		padding: 4px;
		width: 74px;
		height: 100px;
		background: #FFFFFF;
		border: 1px solid #FEE1C3;
		border-radius: 5px;
		display: flex;
		flex-direction: column;
		align-items: center;
		box-sizing: border-box;

		view {
			color: #696969;
		}
	}
}

.danb {
	flex: 1;
}


.closed {
	width: 25px;
	position: absolute;
	right: 10px;
	top: 10px;
}



// 转赠提示
.m_content2 {
		width: 100%;
		font-size: 12px;
		font-weight: bold;
		color: #fff;
		line-height: 20px;
		padding: 10px;

		p {
			color: #fff;
			font-size: 14px;
			text-align: left;
			line-height: 20px;
		}
	}

	.maskClose {
		position: absolute;
		bottom: -50px;
		width: 34px;
		height: 34px;
	}

	.know {
		width: 109px;
		height: 40px;
		background: linear-gradient(90deg, #8BECFF 0%, #F2ACFF 38%, #FDD258 100%);
		border-radius: 7px;
		color: #151515;
		font-size: 14px;
		font-weight: 600;
		margin-top: 20px;
	}

	.centerdd {
		text-align: center !important;
		margin: 10px 0;
		color: #131314 !important;
	}
	
.choosetips {
	color: #fff;
	font-size: 12px;
	margin-bottom: 5px;
}

.confirm {
	width: 60%;
	height: 45px;
	color: #fff;
	margin: 0 auto;
	margin-top: 20px;
	background: #000000 !important;
	border-radius: 21px;
}

.mart5 {
	margin-top: 5px;
}

.explain {
	color: rgba(255, 255, 255, 0.6);
	font-size: 14px;
	margin-top: 10px;
	font-weight: 600;
}

.people {
	position: absolute;
	right: 10px;
	width: fit-content;
	display: flex;
	align-items: center;
	max-width: 200px;

	image {
		width: 30px;
		height: 30px;
		border-radius: 50%;
		margin-right: 5px;
	}

	text {
		flex: 1;
		color: #222;
		font-weight: 600;
		font-size: 12px;
	}
}

</style>
